package com.example.chatzsyproject.fragment;

import android.animation.Animator;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.chatzsyproject.R;
import com.example.chatzsyproject.adapter.CircleOffFriendsAdapter;
import com.example.chatzsyproject.base.ForResultNestedCompatFragment;
import com.example.chatzsyproject.model.CircleOffFriendModel;
import com.example.chatzsyproject.utils.AnimUtil;
import com.example.chatzsyproject.view.AZListView.azlist.AZTitleDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FragmentThree extends ForResultNestedCompatFragment {
    private View rootView;
    private Unbinder unbinder;

    CircleOffFriendsAdapter mAdapter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<CircleOffFriendModel> list = new ArrayList<>();

    @BindView(R.id.im_add)
    ImageView im_add;

    private PopupWindow mPopupWindow;
    private AnimUtil animUtil;
    private float bgAlpha = 1f;
    private boolean bright = false;
    private static final long DURATION = 500;
    private static final float START_ALPHA = 0.7f;
    private static final float END_ALPHA = 1f;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_three, null);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView(rootView);// 控件初始化
        return rootView;
    }

    /**
     * 0~0.5
     *
     * @param vievw
     */
    private void initView(View vievw) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new AZTitleDecoration(new AZTitleDecoration.TitleAttributes(getActivity())));
        mAdapter = new CircleOffFriendsAdapter(R.layout.adapter_circleoffriends, list);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //商品点击事件
            }
        });
        mPopupWindow = new PopupWindow(getActivity());
        animUtil = new AnimUtil();
        im_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
                toggleBright();
            }
        });
        getData();
    }

    public void getData() {
        for (int i = 0; i < 5; i++) {
            CircleOffFriendModel model = new CircleOffFriendModel();
            List<String> imaList = new ArrayList<>();
            imaList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=0650e873d33f1ae914016bd41d1c3201&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201501%2F08%2F20150108164231_t432j.thumb.700_0.png");
            imaList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=5a8f2611f43013847b22eec0493dcb00&imgtype=0&src=http%3A%2F%2Fpic3.zhimg.com%2F50%2Fv2-dae702738ed0a8aa674e5d68d6eaf7e0_hd.jpg");
            imaList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=91bb0746a157533e7031628636eedd02&imgtype=0&src=http%3A%2F%2Fwww.banyingle.com%2Fshop%2Fimages_shop%2F24622%2F2012-01-12%2F69872012112106083970703.jpg");
            model.setName("官御天");
            model.setContent("表示额外的行间距数值，单位通常为dp。如果值为正数表示增加行间距；如果值为负数表示减少行间距:");
            model.setImaList(imaList);
            list.add(model);

            CircleOffFriendModel model1 = new CircleOffFriendModel();
            List<String> imaList1 = new ArrayList<>();
            imaList1.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=91bb0746a157533e7031628636eedd02&imgtype=0&src=http%3A%2F%2Fwww.banyingle.com%2Fshop%2Fimages_shop%2F24622%2F2012-01-12%2F69872012112106083970703.jpg");
            model1.setName("任千行");
            model1.setContent("欻欻欻");
            model1.setImaList(imaList1);
            list.add(model1);

        }
        mAdapter.notifyDataSetChanged();
    }

    private void showPop() {
        mPopupWindow.setContentView(LayoutInflater.from(getActivity()).inflate(R.layout.pop_circle_add, null));
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        mPopupWindow.setAnimationStyle(R.style.pop_add);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAsDropDown(im_add, -100, 0);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                toggleBright();
            }
        });
    }

    private void toggleBright() {
        // 三个参数分别为：起始值 结束值 时长，那么整个动画回调过来的值就是从0.5f--1f的
        animUtil.setValueAnimator(START_ALPHA, END_ALPHA, DURATION);
        animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
            @Override
            public void progress(float progress) {
                // 此处系统会根据上述三个值，计算每次回调的值是多少，我们根据这个值来改变透明度
                bgAlpha = bright ? progress : (START_ALPHA + END_ALPHA - progress);
                backgroundAlpha(bgAlpha);
            }
        });
        animUtil.addEndListner(new AnimUtil.EndListener() {
            @Override
            public void endUpdate(Animator animator) {
                // 在一次动画结束的时候，翻转状态
                bright = !bright;
            }
        });
        animUtil.startAnimator();
    }

    /**
     * 此方法用于改变背景的透明度，从而达到“变暗”的效果
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getActivity().getWindow().setAttributes(lp);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
